home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / Source / Foundation / Common / ZNumbers.h < prev    next >
Text File  |  1997-08-17  |  5KB  |  121 lines

  1. /*
  2.  *  File:       ZNumbers.h
  3.  *  Summary:       Number related functions.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1996-1997 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *  Change History (most recent first):    
  10.  *
  11.  *         <6>     8/18/97    JDJ        Removed Sqrt template function.
  12.  *         <5>     6/20/97    JDJ        Abs() functions use intrinsics on PPC.
  13.  *         <4>     4/27/97    JDJ        Added int and double versions of Random.
  14.  *         <3>     4/15/97    JDJ        Renamed EqualReal Equal. Reformatted.
  15.  *         <2>     3/30/97    JDJ        Added 3 and 4 argument versions of Min and Max.
  16.  *         <1>     1/13/96    JDJ        Created
  17.  */
  18.  
  19. #pragma once
  20.  
  21. #include <ZTypes.h>
  22.  
  23.  
  24. //-----------------------------------
  25. //    Swap, Min, Max
  26. //
  27. template <class T> inline void Swap(T& x, T& y)                                    {T z = x; x = y; y = z;}
  28.  
  29. template <class T> inline T Min(const T& x, const T& y)                            {return x < y ? x : y;}
  30.  
  31. template <class T> inline T Max(const T& x, const T& y)                            {return x > y ? x : y;}
  32.  
  33. template <class T> inline T MinMax(const T& lo, const T& x, const T& hi)        {return Min(Max(x, lo), hi);}
  34.             // Returns x pinned to lo and high.
  35.  
  36.  
  37. template <class T> inline T Min(const T& x, const T& y, const T& z)                {return Min(Min(x, y), z);}
  38.  
  39. template <class T> inline T Min(const T& x, const T& y, const T& z, const T& w)    {return Min(Min(Min(x, y), z), w);}
  40.  
  41. template <class T> inline T Max(const T& x, const T& y, const T& z)                {return Max(Max(x, y), z);}
  42.  
  43. template <class T> inline T Max(const T& x, const T& y, const T& z, const T& w)    {return Max(Max(Max(x, y), z), w);}
  44.  
  45.  
  46. //-----------------------------------
  47. //    Sign
  48. //
  49. template <class T> inline short Sign(T value)        {return value > 0 ? 1 : ((value < 0) ? -1 : 0);}
  50.             // Returns -1, 0, or +1.
  51.     
  52.  
  53. //-----------------------------------
  54. //    Random
  55. //
  56. int             Random(int max);
  57. double             Random(double max);
  58. long             Random(long max);
  59.                 // Returns a random number in [0, max).
  60.  
  61. int             Random(int min, int max);
  62. long             Random(long min, long max);
  63. double             Random(double min, double max);
  64.                 // Returns a random number in [min, max).
  65.     
  66. bool             FlipCoin();
  67.                 // Very fast heads or tails random number generator.
  68.             
  69. void             SetRandomSeed(long seed);
  70.                 // Note that the seed is inited to a psuedo-random value.
  71.  
  72.  
  73. //-----------------------------------
  74. //    Misc
  75. //
  76. inline bool     InBounds(long lo, long value, long hi)    {return (lo <= value) && (value <= hi);}
  77.                 // Returns true if lo <= value <= hi.
  78.  
  79. inline short     MulDiv(short value, short num, short denom)    {return (short) ((long) value*num/denom);}
  80. inline int         MulDiv(int value, int num, int denom)        {return value*num/denom;}
  81. inline ulong     MulDiv(ulong value, ulong num, ulong denom)    {return value*num/denom;}
  82.                 // Returns value*num/denom. Uses longs to avoid overflows.    
  83.     
  84. bool             Equal(double x, double y, double tolerance = 1.0e-6, double typical = 1.0);
  85.                 // Returns true if the difference between the numbers is less
  86.                 // than tolerance or the numbers are much smaller than typical.
  87.  
  88. inline bool     Equal(short x, short y)                    {return x == y;}
  89. inline bool     Equal(int x, int y)                        {return x == y;}
  90. inline bool     Equal(long x, long y)                    {return x == y;}
  91.  
  92. inline bool     Even(long x)                            {return (x & 1) == 0;}
  93. inline bool     Even(ulong x)                            {return (x & 1) == 0;}
  94. inline bool     Odd(long x)                                {return (x & 1) == 1;}
  95. inline bool     Odd(ulong x)                            {return (x & 1) == 1;}
  96.  
  97.  
  98. // ===================================================================================
  99. //    Inlines
  100. // ===================================================================================
  101. #if powerc
  102.     inline short    Abs(short value)        {return (short) __abs(value);}
  103.     inline int        Abs(int value)            {return (int) __abs(value);}
  104.     inline long        Abs(long value)            {return __labs(value);}
  105.     inline float    Abs(float value)        {return (float) __fabs(value);}
  106.     inline double    Abs(double value)        {return __fabs(value);}
  107. #else
  108.     inline short    Abs(short value)        {return value < 0 ? -value : value;}
  109.     inline int        Abs(int value)            {return value < 0 ? -value : value;}
  110.     inline long        Abs(long value)            {return value < 0 ? -value : value;}
  111.     inline float    Abs(float value)        {return value < 0 ? -value : value;}
  112.     inline double    Abs(double value)        {return value < 0 ? -value : value;}
  113. #endif
  114.  
  115. inline short    Random(short max)            {return (short) Random((long) max);}
  116. inline ushort    Random(ushort max)            {return (ushort) Random((long) max);}
  117. inline int         Random(int max)                {return Random((long) max);}
  118. inline int         Random(int min, int max)    {return Random((long) min, (long) max);}
  119.  
  120.  
  121.